﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace Workflows.Designer.OfficeUtil
{
    public class HeaderFooterWriter
    {
        #region fields
        private Application _App;
        /// <summary></summary>
        public Application App
        {
            get
            {
                return this._App;
            }
            set
            {
                this._App = value;
            }
        }

        private Document _Doc;
        /// <summary></summary>
        public Document Doc
        {
            get
            {
                return this._Doc;
            }
            set
            {
                this._Doc = value;
            }
        }

        private Range _PageHeader;
        /// <summary></summary>
        public Range PageHeader
        {
            get
            {
                return this._PageHeader;
            }
            set
            {
                this._PageHeader = value;
            }
        }

        private Range _PageFoter;
        /// <summary></summary>
        public Range PageFoter
        {
            get
            {
                return this._PageFoter;
            }
            set
            {
                this._PageFoter = value;
            }
        }
        #endregion

        public HeaderFooterWriter(Application app, Document doc)
        {
            this._App = app;
            this._Doc = doc;
        }

        private Selection SetFocusToPageHeader()
        {
            this._App.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
            return this._App.ActiveWindow.Selection;
        }

        private Selection SetFocusToPageFooter()
        {
            this._App.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekPrimaryFooter;
            return this._App.ActiveWindow.Selection;
        }

        private void ResetFocus()
        {
            this._App.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
        }

        public void InsertPageHeaderContent(string headerContent)
        {
            Selection selection = this.SetFocusToPageHeader();
            selection.TypeText(headerContent);
            this.ResetFocus();
        }

        public void InsertPageFooterContent(string footerContent)
        {
            Selection selection = this.SetFocusToPageFooter();
            selection.TypeText(footerContent);
            this.ResetFocus();
        }
    }
}
